package com.airport.ticket.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.airport.ticket.common.UrlUtil;
import com.airport.ticket.dao.UserInfoMapper;
import com.airport.ticket.entity.UserInfo;
import com.airport.ticket.entity.UserInfoExample;
import com.airport.ticket.entity.UserInfoExample.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/admin")
public class UserController {

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(value = "/user_list.html")
    public String userList(ModelMap map, String keyword, String pageNow, String pageSize, String errorMsg){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "用户管理");
		map.addAttribute("keyword", keyword);
		
		if(errorMsg!=null && !"".equals(errorMsg)) {
			map.addAttribute("errorMsg", errorMsg);
		}
		
		if(pageNow==null || "".equals(pageNow) || Integer.parseInt(pageNow) < 1) {
			pageNow = "1";
		}
		
		if(pageSize==null || "".equals(pageSize) || Integer.parseInt(pageSize) < 1) {
			pageSize = "20";
		}
		
		UserInfoExample condition = new UserInfoExample();
		Criteria ct = condition.createCriteria();
		if(keyword!=null && !"".equals(keyword)) {
			ct.andFullNameLike("%"+keyword+"%");
		}
		ct.andUserTypeEqualTo("2");
		condition.setOrderByClause("id DESC");
		PageHelper.startPage(Integer.parseInt(pageNow),Integer.parseInt(pageSize));
		List<UserInfo> resultList = userInfoMapper.selectByExample(condition);
		
		if(resultList==null || resultList.size()==0){
			map.addAttribute("retCode", "99");
			map.addAttribute("pageNow", pageNow);
			map.addAttribute("pageSize", pageSize);
			map.addAttribute("totalPage", "0");
			return "admin/user/user_list";
        }
		
		PageInfo<UserInfo> pageInfo = new PageInfo<>(resultList);
		map.addAttribute("pageNow", pageNow);
		map.addAttribute("pageSize", pageSize);
		map.addAttribute("totalPage", pageInfo.getPages());
		map.addAttribute("resultList", resultList);
			
        return "admin/user/user_list";
    }
	
	@RequestMapping(value = "/add_user.html")
    public String adduser(ModelMap map){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "新增用户");		
		return "admin/user/add_user";
    }
	

	@PostMapping(value = "/do_add_user.html")
    public String doAdduser(ModelMap map, UserInfo user){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "新增用户");	
		
		/**
		 * 判断用户名是否重复
		 */
		UserInfoExample condition = new UserInfoExample();
		condition.createCriteria().andUsernameEqualTo(user.getUsername());
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(condition);
		if(userInfoList!=null && userInfoList.size() > 0) {
			return "redirect:/admin/user_list.html?errorMsg="+UrlUtil.parseUtf8("用户名已存在，请更换");
		}
		
		/**
		 * 插入用户表
		 */
		user.setUserType("2");
		user.setCreatetime(new Date());
		if(user.getAmount()==null) {
			user.setAmount(new BigDecimal("1000.00"));
		}
		userInfoMapper.insertSelective(user);
		
		return "redirect:/admin/user_list.html";
    }
	
	@RequestMapping(value = "/update_user.html")
    public String updateuser(ModelMap map,String id){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "编辑用户");	
		
		/**
		 * 判断用户是否存在
		 */
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(userInfo==null || userInfo.getId()==null || "".equals(userInfo.getId())) {
			return "redirect:/admin/user_list.html?errorMsg="+UrlUtil.parseUtf8("用户不存在");
		}
		
		map.addAttribute("userInfo", userInfo);		
		return "admin/user/update_user";
    }
	

	@PostMapping(value = "/do_update_user.html")
    public String doUpdateuser(ModelMap map, UserInfo user){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "编辑用户");
		
		/**
		 * 判断用户是否存在
		 */
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(user.getId());
		if(userInfo==null || userInfo.getId()==null || "".equals(userInfo.getId())) {
			return "redirect:/admin/user_list.html?errorMsg="+UrlUtil.parseUtf8("用户不存在");
		}
		
		/**
		 * 判断用户名是否重复
		 */
		UserInfoExample condition = new UserInfoExample();
		condition.createCriteria().andUsernameEqualTo(user.getUsername());
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(condition);
		if(userInfoList!=null && userInfoList.size() > 0 &&  !userInfoList.get(0).getId().equals(user.getId())) {
			return "redirect:/admin/user_list.html?errorMsg="+UrlUtil.parseUtf8("用户名已存在，请更换");
		}
		
		/**
		 * 更新用户表
		 */
		user.setUserType("2");
		if(user.getAmount()==null) {
			user.setAmount(new BigDecimal("1000.00"));
		}
		userInfoMapper.updateByPrimaryKeySelective(user);

		return "redirect:/admin/user_list.html";
    }
	

	@RequestMapping(value = "/do_delete_user.html")
    public String doDeleteuser(ModelMap map, UserInfo user){
		map.addAttribute("nav", "1");
		map.addAttribute("title", "删除用户");
		
		/**
		 * 判断用户是否存在
		 */
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(user.getId());
		if(userInfo==null || userInfo.getId()==null || "".equals(userInfo.getId())) {
			return "redirect:/admin/user_list.html?errorMsg="+UrlUtil.parseUtf8("用户不存在");
		}
		
		
		/**
		 * 删除用户
		 */
		userInfoMapper.deleteByPrimaryKey(user.getId());

		return "redirect:/admin/user_list.html";
    }
	
}
